package com.chixing.day09_innerclass.word;

import java.util.Arrays;

public class StringDemo {
    public static void main(String[] args) {
        String word = "Strings are constant; their values cannot be changed after they  are created";
        //将指定字符串全部转换成大写
        String s1 = word.toUpperCase();
        System.out.println(s1);

        //将指定字符串全部转换成小写
        String s2 = word.toLowerCase();
        System.out.println(s2);

        //获得指定字符串的第一个字符
        char c1 = word.charAt(0);
        System.out.println(c1);

        //获得指定字符串的每一个字符
        char[] chars = word.toCharArray();
        System.out.println(Arrays.toString(chars));

        //获得指定字符串的字符长度
        int a1 = word.length();
        System.out.println(a1);

        //将两个字符串拼接成一个字符串
        String str1 = "hello";
        String str2 = "world";
        String str3 = str1 + str2;
        System.out.println(str3);

        //将指定字符串中的a字母转换成Z
        String s3 = word.replace("a","Z");
        System.out.println(s3);

        //获得指定字符串中的a字母出现的下标
        int a2 = word.indexOf("a");
        System.out.println(a2);

        int n =0;
        do {
            int s = word.indexOf("a",n);
            n = s+1;
            System.out.println(s);
        }while (word.indexOf("a",n)!=-1);


        //获得字符串"clone-finalize-getClass-notify-notifyAll-wait" 中的每个单词
        String str4 = "clone-finalize-getClass-notify-notifyAll-wait";
        String[] strings = str4.split("-");
        System.out.println(Arrays.toString(strings));
        System.out.println(strings[0]);

        //将数值76转换成字符串
        String s4 = Integer.toString(76);
        System.out.println(s4);

        String st1  = String.valueOf(76);
        String st2  = String.valueOf(57.8);

        //将数值57.8转换成字符串
        String s5 = Double.toString(57.8);
        System.out.println(s5);

        /*字符串加密解密**
        Welcome
        加密方法：
        将第一个字母调换到最后，然后每个字母加3
       （1）实现加密，并测试
       （2）给定某一加密后的字符串，并解密还原成初始字符串*/
        String string = "Zelcome";
        //加密
        char[] chars1 = string.toCharArray();
        for (int i = 0;i<chars1.length;i++){
            if ((chars1[i]>='X'&&chars1[i]<='Z')||(chars1[i]>='x'&&chars1[i]<='z'))
                chars1[i]= (char) (chars1[i]+3-26);
            else
                chars1[i] = (char) (chars1[i] + 3);
        }
        char temp = chars1[0];
        chars1[0] = chars1[chars1.length-1];
        chars1[chars1.length-1] = temp;
        System.out.println(chars1);

        //解密
        for (int i = 0;i<chars1.length;i++){
            if ((chars1[i]>='A'&&chars1[i]<='C')||(chars1[i]>='a'&&chars1[i]<='c'))
                chars1[i] = (char) (chars1[i] - 3 + 26);
            else
                chars1[i] = (char) (chars1[i] - 3);
        }
        char temp1 = chars1[0];
        chars1[0] = chars1[chars1.length-1];
        chars1[chars1.length-1] = temp1;
        System.out.println(chars1);


        //优惠券
        String string1 = "1Ct3r4ip_ti4C6k9Et^";
        char[] chars2 = string1.toCharArray();
        String string2 = "";
        for (int i = 0;i<chars2.length;i++){
            if (chars2[i]>='A'&&chars2[i]<='z'){
                string2+=chars2[i];
            }
        }
        System.out.println(string2);
    }


}
